# 线程同步
# 方式1 join
# 方式2 互斥锁
from threading import Thread, Lock

print("-------------- 互斥锁 acquire / release --------------")
a = 0
b = 1000000
# 循环次就给全局变量 a+1

lock = Lock()


def plus():
    lock.acquire()  # 上锁
    for i in range(b):
        global a  # 局部变量不能修改全局变量
        a += 1
    print("plus :", a)
    lock.release()  # 解锁


def increase():
    lock.acquire()
    for i in range(b):
        global a  # 局部变量不能修改全局变量
        a += 1
    print("increase :", a)
    lock.release()


if __name__ == "__main__":
    p1 = Thread(target=plus)
    p2 = Thread(target=increase)
    p1.start()
    p2.start()

# 互斥锁是多个线程一起去抢, 抢到锁的线程先去执行.
# 死锁: 一直等待对方释放锁的情景
# 死锁会造成应用程序停止响应,不再处理其他任务,
